package test04datainput;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class _FileoutStream {

    public static void main(String[] args) throws Exception {

        String inputFilePath = _FileoutStream.class.getClassLoader().getResource("").getPath();
        //1.创建数据源
        File src = new File(inputFilePath);

        String outputFilePath = _FileoutStream.class.getClassLoader().getResource("").getPath();
        //2.创建对象表示目的地
        File dest = new File(outputFilePath, "test2");

        //3.开始拷贝
        copydir(src, dest);

        //4.拷贝完成
        System.out.println("OK!");

    }

    private static void copydir(File src, File dest) throws Exception {
        if (!dest.exists()) {
            dest.mkdirs();
        }

        //递归
        //1.进入数据源
        File[] files = src.listFiles();
        //依次便利数组
        for (File file : files) {
            if (file.isFile()) {
                //如果是文件就拷贝
                try (FileInputStream fis = new FileInputStream(file);
                     FileOutputStream fos = new FileOutputStream(new File(dest, file.getName()));
                ) {

                    byte[] bytes = new byte[512];
                    int len;
                    while ((len = fis.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }

                }
            } else {
                //如果是文件夹就递归
                copydir(file, new File(dest, file.getName()));
            }
        }
    }
}

